package cn.dida.dao;

import java.util.Date;
import java.util.HashMap;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import cn.dida.model.DeliveryInfo;

/**
 * <p>物流配送信息  数据交互类</p>
 * @author lisha
 * @date 2016-11-21 10:35
 * */
@Repository
public class DeliveryInfoDao extends DaoBase {
	
	
	/**
	 * 修改库存
	 * @param tprize
	 * @param n1
	 * @param n2
	 * @param n3
	 * @return
	 */
	public Integer updatePrizeStock(int tprize, int n1, int n2, int n3) {
		// TODO Auto-generated method stub
		String sql = "update prize set stock =stock + ?,win_nub =win_nub + ?,cash_nub =cash_nub + ? where id = ?  ";

		return jdbcTemplate.update(sql, n1, n2, n3, tprize);
	}
	/**
	 * 增加配送信息
	 * @param info 配送信息实体类
	 * @return int 受影响的记录行数
	 * @date 2016-11-21 10:37
	 * */
	public int insert(DeliveryInfo info){
		
		 String sql = "insert into delivery_info(activity_winning_id, logistics_company_id, waybill_number,delivery_time,"
		 		+ " c_time, c_admin"
		 		+ ") values (:winId, :commpanyId, :wayNumber, :deTime,  :cTime, "
		 		+ ":cAdmin)";
		 	Long current = new Date().getTime();
	        HashMap<String, Object> paramMap = new HashMap<>();
	        paramMap.put("winId", info.getActivityWinningId());
	        paramMap.put("commpanyId", info.getLogisticsCompanyId());
	        paramMap.put("wayNumber", info.getWaybillNumber());
	        paramMap.put("deTime", current);
	        paramMap.put("cTime", current);
	        paramMap.put("cAdmin", info.getcAdmin());
	        return namedParameterJdbcTemplate.update(sql, paramMap);
	}
	/**
	 * 更新物流配送信息
	 * @param info 配送信息实体类
	 * @return int 受影响的记录行数
	 * @date 2016-11-21 10:52
	 * */
	public int update(DeliveryInfo info) {
        String sql = "update delivery_info set activity_winning_id=:winid, logistics_company_id=:commpanyid, "
        		+ "waybill_number=:waynumber, delivery_time=:detime "
        		+ " where id=:id";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("winid", info.getActivityWinningId());
        paramMap.put("commpanyid", info.getLogisticsCompanyId());
        paramMap.put("waynumber", info.getWaybillNumber());
        paramMap.put("detime", info.getDeliveryTime());
        paramMap.put("id", info.getId());
        return namedParameterJdbcTemplate.update(sql, paramMap);
    }
	
	/**
	 * 根据兑奖记录id获取配送信息
	 * @param winid 兑奖id
	 * @return DeliveryInfo 物流配送信息
	 * @date 2016-11-21 10:48
	 * */
	public DeliveryInfo getInfoByWinid(Integer winid) {
        String sql = "select * from delivery_info where activity_winning_id = ?";
        try {
        	DeliveryInfo info = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<DeliveryInfo>(DeliveryInfo.class), winid);
            return info;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
}
